package com.thrd.taiexhibition.fresco;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.thrd.taiexhibition.utils.Constant;

public class FrescoUtils {
    private static FrescoUtils sInstance = null;
    /**
     * 构造器私有化
     */
    private FrescoUtils() {

    }

    /**
     * 获得单例的对象
     * @return
     */
    public static FrescoUtils getInstance(){
        if (sInstance == null){
            synchronized (FrescoUtils.class){
                if (sInstance == null){

                    sInstance = new FrescoUtils();
                }
            }
        }
        return sInstance;
    }




    ///////////////////////////////////////////////////////////////////////////
    // 加载图片的样式设置
    ///////////////////////////////////////////////////////////////////////////


    /**
     * 设置Fresco框架的进度条加载图片
     *
     * @return
     */
    //fresco的特性
    private   GenericDraweeHierarchy getHierarchy(Context mContext,RoundingParams roundingParams) {

        GenericDraweeHierarchy genericDraweeHierarchy = new GenericDraweeHierarchyBuilder(mContext.getResources())

//                .setProgressBarImage(context.getResources().getDrawable(R.drawable.ic_image_load))
//                .setProgressBarImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE)
                .setRoundingParams(roundingParams)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .build();
        return genericDraweeHierarchy;

    }


    /**
     * @param view   SimpleDraweeView 的对象
     * @param imageUrl  要加载的网络图片的地址
     * @return  返回PipelineDraweeController控制器的对象
     */
    public  PipelineDraweeController getController(SimpleDraweeView view, String imageUrl) {
        //渐进式加载
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageUrl))
                .setProgressiveRenderingEnabled(true).build();
        //初始化控制器
        PipelineDraweeController pipelineDraweeController = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(view.getController())
                .setTapToRetryEnabled(true).build();

        return pipelineDraweeController;

    }


    /**
     * 带进度条的加载
     * @param view
     * @param imageUrl
     */
    public void setProgressLoad(Context mContext,SimpleDraweeView view, String imageUrl){
        GenericDraweeHierarchy mGenericDraweeHierarchy=  getHierarchy(mContext,null);
        mGenericDraweeHierarchy.setProgressBarImage(new ImageLoadingDrawable());
        view.setHierarchy(mGenericDraweeHierarchy);
        //设置要加载的网络图片的地址
        view.setImageURI(Uri.parse(imageUrl));
    }


    /**
     * 带背景的加载
     * @param view
     * @param imageUrl
     */
    public void setBackgroundLoad(Context mContext,SimpleDraweeView view, String imageUrl){
        GenericDraweeHierarchy mGenericDraweeHierarchy=  getHierarchy(mContext,null);
  //      mGenericDraweeHierarchy.setPlaceholderImage(R.drawable.fresco_bg);
        view.setHierarchy(mGenericDraweeHierarchy);
        //设置要加载的网络图片的地址
        view.setImageURI(Uri.parse(imageUrl));
    }


    /**
     * 带背景圆角的加载
     * @param view
     * @param imageUrl
     */
    public void setBackgroundRoundLoad(Context mContext,int rounding,SimpleDraweeView view, String imageUrl){
        GenericDraweeHierarchy mGenericDraweeHierarchy=  getHierarchy(mContext,null);
        RoundingParams roundingParams = RoundingParams.fromCornersRadius(rounding);
        mGenericDraweeHierarchy.setRoundingParams(roundingParams);
     //   mGenericDraweeHierarchy.setPlaceholderImage(R.drawable.fresco_bg);
        view.setHierarchy(mGenericDraweeHierarchy);
        //设置要加载的网络图片的地址
        view.setImageURI(Uri.parse(imageUrl));
    }

    /**
     * 默认加载
     * @param view
     * @param imageUrl
     */
    public void setLoad(SimpleDraweeView view, String imageUrl){
        //设置要加载的网络图片的地址
        if(view!=null&& !TextUtils.isEmpty(imageUrl)){
            view.setImageURI(Uri.parse(imageUrl));
        }else if(TextUtils.isEmpty(imageUrl)){
            view.setImageURI(Uri.parse(""));
        }

    }




    /**
     * 渐变加载
     * @param view
     * @param imageUrl
     */
    public void setGradientLoad(SimpleDraweeView view, String imageUrl){
        //设置要加载的网络图片的地址
        view.setImageURI(Uri.parse(imageUrl));
    }



    /**
     * 清除缓存
     */
   public void clearMemory(){
       ImagePipelineFactory.getInstance().getImagePipeline().clearMemoryCaches();
       System.gc();
   }


//   public void downloadImage(FrescoImage mFrescoImage,Uri uri, BaseControllerListener mBitmapDataSubscriber){
//
////       ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri).setProgressiveRenderingEnabled(true).build();
////       ImagePipeline imagePipeline = Fresco.getImagePipeline();
////       DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this);
////       dataSource.subscribe(mBitmapDataSubscriber, CallerThreadExecutor.getInstance());
////       GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(mContext.getResources())
////               .setFadeDuration(300)
//////                .setPlaceholderImage(defaultDrawable)
//////                .setFailureImage(defaultDrawable)
////               .setProgressBarImage(new ImageLoadingDrawable())
////               .build();
////       DraweeHolder<GenericDraweeHierarchy> draweeHolder = DraweeHolder.create(hierarchy, mContext);
////
////       PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
////               .setOldController(draweeHolder.getController())
////               .setImageRequest(imageRequest)
////               .build();
////       controller.onClick();
//       GenericDraweeHierarchy mGenericDraweeHierarchy=  getHierarchy(mFrescoImage.getContext());
//       mGenericDraweeHierarchy.setProgressBarImage(new ImageLoadingDrawable());
//       mFrescoImage.setHierarchy(mGenericDraweeHierarchy);
//       DraweeController controller = Fresco.newDraweeControllerBuilder()
//               .setUri(uri)
//               .setAutoPlayAnimations(true)
//               .setControllerListener(mBitmapDataSubscriber)
//               .build();
//
//       mFrescoImage.setController(controller);
//   }


    /**
     * 带进度条的加载  产品加载
     * @param view
     * @param imageUrl
     */
    public void setProgressLoadProduct(Context mContext,SimpleDraweeView view, String imageUrl){
        GenericDraweeHierarchy genericDraweeHierarchy = new GenericDraweeHierarchyBuilder(mContext.getResources())
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .build();
        genericDraweeHierarchy.setProgressBarImage(new ImageLoadingDrawable());
        view.setHierarchy(genericDraweeHierarchy);
        //设置要加载的网络图片的地址
        view.setImageURI(Uri.parse(imageUrl));
    }

    /**
     * 按大小加载
     * @param uri
     * @param draweeView
     * @param width
     * @param height
     */
    public  void loadResizeOptions(Uri uri,SimpleDraweeView draweeView,int width,int height,BaseControllerListener mBitmapDataSubscriber
                            ){
        ImageRequest request =
                ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(width,height))
                        //缩放,在解码前修改内存中的图片大小, 配合Downsampling可以处理所有图片,否则只能处理jpg,
                        // 开启Downsampling:在初始化时设置.setDownsampleEnabled(true)
                        .setProgressiveRenderingEnabled(true)//支持图片渐进式加载
                        .setAutoRotateEnabled(true) //如果图片是侧着,可以自动旋转
                        .build();

        PipelineDraweeController controller =
                (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setImageRequest(request)
                        .setOldController(draweeView.getController())
                        .setAutoPlayAnimations(true) //自动播放gif动画
                        .setControllerListener(mBitmapDataSubscriber)
                        .build();

        GenericDraweeHierarchy genericDraweeHierarchy = new GenericDraweeHierarchyBuilder(draweeView.getResources())
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .build();
        genericDraweeHierarchy.setProgressBarImage(new ImageLoadingDrawable());
        draweeView.setHierarchy(genericDraweeHierarchy);
        draweeView.setController(controller);
    }

    public void initFresco(Context mContext){
        ImagePipelineConfig.Builder imagePipelineConfigBuilder = ImagePipelineConfig.newBuilder(mContext);
        imagePipelineConfigBuilder.setMainDiskCacheConfig(DiskCacheConfig.newBuilder(mContext)
                .setBaseDirectoryPath(mContext.getExternalCacheDir())//设置磁盘缓存的路径
                .setBaseDirectoryName("image")//设置磁盘缓存文件夹的名称
                .setMaxCacheSize(1024 * 1024 * 1024)//设置磁盘缓存的大小
                .build());
        ImagePipelineConfig config = imagePipelineConfigBuilder.build();
        Fresco.initialize(mContext, config);//初始化
    }



    /**
     * 带进度条的加载
     * @param view
     * @param imageUrl
     */
    public void setPrefixProgressLoad(Context mContext,SimpleDraweeView view, String imageUrl){
//        GenericDraweeHierarchy mGenericDraweeHierarchy=  getHierarchy(mContext,null);
//        mGenericDraweeHierarchy.setPlaceholderImage(R.drawable.system_image_placeholder);
//        mGenericDraweeHierarchy.setRetryImage(R.drawable.system_image_error);

       // mGenericDraweeHierarchy.setProgressBarImage(new ImageLoadingDrawable());
//        view.setHierarchy(mGenericDraweeHierarchy);

        //设置要加载的网络图片的地址
          view.setImageURI(Uri.parse(Constant.IMAGE+imageUrl));

    }

    /**
     * 带进度条的加载
     * @param view
     * @param imageUrl
     */
    public void setPrefixProgressLoad(Context mContext,SimpleDraweeView view, String imageUrl,RoundingParams roundingParams){
//        GenericDraweeHierarchy mGenericDraweeHierarchy=  getHierarchy(mContext,roundingParams);
//        mGenericDraweeHierarchy.setPlaceholderImage(R.drawable.system_image_error);
//        mGenericDraweeHierarchy.setProgressBarImage(new ImageLoadingDrawable());
//        view.setHierarchy(mGenericDraweeHierarchy);
        //设置要加载的网络图片的地址
        view.getHierarchy().setRoundingParams(roundingParams);
        view.setImageURI(Uri.parse(Constant.IMAGE+imageUrl));

    }




}
